home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / cenvid9.zip / CMM_VS_C.DOC < prev    next >
Text File  |  1994-03-09  |  17KB  |  401 lines

  1.                      CEnvi Shareware Manual, Chapter 3:
  2.                       Cmm versus C, for C Programmers
  3.  
  4.                      CEnvi unregistered version 1.009
  5.                                9 March 1994
  6.  
  7.                        CEnvi Shareware User's Manual
  8.  
  9.           Copyright 1993, Nombas, All Rights Reserved.
  10.           Published by Nombas, P.O. Box 875, Medford, MA 02155 USA
  11.           (617)391-6595
  12.  
  13.           Thank you for trying this shareware version of CEnvi from Nombas,
  14.           a member of the Association of Shareware Professionals (ASP).
  15.  
  16. 3.  Cmm versus C: The Cmm language for C programmers
  17.  
  18.           This chapter is for those who already know how to program in the
  19.           C language.  This chapter describes only those elements of Cmm
  20.           that differ from standard C, and so if you don't already
  21.           understand C, then this shouldn't have much meaning for you.
  22.           Non-C programmers should instead look at the previous chapter.
  23.  
  24.           Since it is assumed that readers of this chapter are already
  25.           knowledgeable in C, only those aspects of Cmm that differ from C
  26.           are described here.  If it's not mentioned here, then assume that
  27.           Cmm behavior will be standard C.
  28.  
  29.           Deviations from C are a result of these two harmonious Cmm
  30.           directives: Convenience and Safety.  Cmm is different from C
  31.           where the change makes Cmm more convenient for small programs,
  32.           command-line code, or scripting files, or if unaltered C rules
  33.           encourage coding that is potentially unsafe.
  34.  
  35. 3.1.  C Minus Minus
  36.  
  37.           Cmm is "C minus minus" where the minuses are Type Declarations
  38.           and Pointers.  If you already know C and can remember to forget
  39.           these two aspects of C (I repeat, no Type Declarations and no
  40.           Pointers) then you know Cmm.  If you were to take C code, and
  41.           delete all the lines, code-words, and symbols that either declare
  42.           data types or explicitly point to data, then you would be left
  43.           with Cmm code; and although you would be removing bytes of source
  44.           code, you would not be removing capabilities.
  45.  
  46.           All of the details below that compare Cmm against C follow from
  47.           the general rule:
  48.             *Cmm is C minus Type Declarations and minus Pointers.
  49.  
  50. 3.2.  Data Types
  51.  
  52.           The only recognized data types are Float, Long, and Byte.  The
  53.           words "Float", "Long", and "Byte" do not appear in Cmm source
  54.           code; instead, the data types are determined by context.  For
  55.           instance 6 is a Long, 6.6 is a Float, and '6' is a Byte.  Byte is
  56.           unsigned, and the other types are signed.
  57.  
  58. 3.3.  Automatic Type Declaration
  59.  
  60.           There are no type declarators and no type casting.  Types are
  61.           determined from context.  If the code says "i=6" then i is a
  62.           Long, unless a previous statement has indicated otherwise.
  63.  
  64.           For instance, this C code:
  65.               int Max(int a,int b)
  66.               {
  67.                 int result;
  68.                 result = ( a < b ) ? b : a ;
  69.                 return result;
  70.               }
  71.           could become this Cmm code:
  72.               Max(a,b)
  73.               {
  74.                 result = ( a < b ) ? b : a ;
  75.                 return result;
  76.               }
  77.  
  78. 3.4.  Array Representation
  79.  
  80.           Arrays are used in Cmm much like they are in C, except that they
  81.           are stored differently: a first-order array (e.g., a string) is
  82.           stored in consecutive bytes in memory, but arrays of arrays are
  83.           not in consecutive memory locations.  The C declaration "char
  84.           c[3][3];" would state that there are nine consecutive bytes in
  85.           memory.  In Cmm a similar statement such as "c[2][2] = 'A'" would
  86.           tell you that there are (at least) three arrays of characters,
  87.           and the third array of arrays has (at least) three characters in
  88.           it; and although the characters in c[0] are in consecutive bytes,
  89.           and the characters in c[1] are in consecutive bytes, the two
  90.           arrays c[0] and c[1] are not necessarily adjacent in memory.
  91.  
  92. 3.4.1   Array Arithmetic
  93.  
  94.           When one array is assigned to the other, as in:
  95.               foo = "cat";
  96.               goo = foo;
  97.           then both variables define the same array and start at the same
  98.           offset 0.  In this case, if foo[2] is changed then you will find
  99.           that goo[2] has also been changed.
  100.  
  101.           Integer addition and subtraction can also be performed on arrays.
  102.           Array addition or subtraction sets where the array is based.  By
  103.           altering the previous code segment to:
  104.               foo = "cat";
  105.               goo = foo + 1;
  106.           goo and foo would now be arrays containing the same data, except
  107.           that now goo is based one element further, and foo[2] is now the
  108.           same data as goo[1].
  109.  
  110.           To demonstrate:
  111.               foo = "cat";  // foo[0] is 'c', foo[1] = 'a'
  112.               goo = foo + 1;// goo[0] is 'a', goo[-1] = 'c'
  113.               goo[0] = 'u'; // goo[0] is 'u', foo[1] = 'u', foo is "cut"
  114.               goo++;        // goo[0] is 't', goo[-2] = 'c'
  115.               goo[-2] = 'b' // goo[0] is 't', foo[0] = 'b', foo is "but"
  116.  
  117. 3.4.2   Automatic Array Allocation
  118.  
  119.           Arrays are dynamic, and any index, (positive or negative) into an
  120.           array is always valid.  If an element of an array is referred to,
  121.           then the Cmm must see to it that such an element will exist.  For
  122.           instance if the first statement in the Cmm source code is "foo[4]
  123.           = 7;" then the Cmm interpreter will make an array of 5 integers
  124.           referred to by the variable foo.  If a statement further on
  125.           refers to "foo[6]" then the Cmm interpreter will grow foo, if it
  126.           has to, to ensure that the element foo[6] exists.  This works
  127.           with negative indices as well.  When you refer to foo[-10], then
  128.           foo is grown in the other direction if it needs to be, but foo[4]
  129.           will still refer to that "7" you put there earlier.  Arrays can
  130.           reach any dimension order, so that foo[6][7][34][-1][4] is a
  131.           valid value.
  132.  
  133. 3.5.  Structures
  134.  
  135.           Structures are created dynamically, and their elements are not
  136.           necessarily contiguous in memory.  When CEnvi comes across the
  137.           statement 'foo.animal = "dog"' it creates a structure element of
  138.           foo that is referred to as "animal" and is an array of
  139.           characters, and this "animal" variable is thereafter carried
  140.           around with "foo" (much like a stem variable in REXX).  The
  141.           resulting code looks very much like regular C code, except that
  142.           there is not a separate structure definition anywhere.
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.           This C code:
  153.  
  154.               struct Point {
  155.                 int Row;
  156.                 int Column;
  157.               };
  158.  
  159.               struct Square {
  160.                 struct Point BottomLeft;
  161.                 struct Point TopRight;
  162.               };
  163.  
  164.               void main()
  165.               {
  166.                 struct Square sq;
  167.                 int Area;
  168.                 sq.BottomLeft.Row = 1;
  169.                 sq.BottomLeft.Column = 15;
  170.                 sq.TopRight.Row = 82;
  171.                 sq.TopRight.Column = 120;
  172.                 Area = AreaOfASquare(sq);
  173.               }
  174.  
  175.               int AreaOfASquare(struct Square s)
  176.               {
  177.                 int width, height;
  178.                 width = s.TopRight.Column - s.BottomLeft.Column + 1;
  179.                 height = s.TopRight.Row - s.BottomLeft.Row + 1;
  180.                 return( length * height );
  181.               }
  182.  
  183.           can be changed into the equivalent Cmm code simply be removing
  184.           declaration lines, resulting in:
  185.  
  186.               main()
  187.               {
  188.                 sq.BottomLeft.Row = 1;
  189.                 sq.BottomLeft.Column = 15;
  190.                 sq.TopRight.Row = 82;
  191.                 sq.Top